package com.aorise.service.migration.impl;

import com.aorise.exception.ServiceException;
import com.aorise.mapper.migration.*;
import com.aorise.model.migration.*;
import com.aorise.service.camera.CameraGroupService;
import com.aorise.service.migration.MigrationService;
import com.aorise.utils.ConstDefine;
import com.aorise.utils.StatusDefine;
import com.aorise.utils.StatusDefineMessage;
import com.aorise.utils.page.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:cat
 * @Desicription: 迁建记录接口服务实现类
 * @Date:Created in 2019-06-01
 * @Modified By:
 */
@Service
public class MigrationServiceImpl implements MigrationService {

    @Autowired
    private MigrationMapper migrationMapper;
    @Autowired
    private MigrationCameraMapper migrationCameraMapper;
    @Autowired
    private MigrationInformMapper migrationInformMapper;
    @Autowired
    private CameraGroupService cameraGroupService;

    /**
     * @Author:cat
     * @Description 分页查询迁建记录信息
     * @params: map 查询条件
     * @params: page 分页
     * @Date:2019-06-03
     * @Return: List<MigrationModel> 迁建记录信息集合
     * @Modified By:
     */
    @Override
    public List<MigrationModel> getAllMigrationListByPage(Map<String, Object> map, Page page) {
        List<MigrationModel> cameraModels = null;

        /**查询总条数*/
        Long count = migrationMapper.getAllMigrationListTotalByPage(map);
        if (count > 0) {
            page.setTotalCount(count);
            page.setTotalPage(count, page.getEveryPage());
            if (page.getCurrentPage() > 0 && page.getEveryPage() > 0) {
                /**查询分页条数*/
                cameraModels = migrationMapper.getAllMigrationListByPage(map);
            }
        } else {
            page.setTotalCount(0L);
        }
        return cameraModels;
    }

    /**
     * @Author:cat
     * @Description 根据ID查询迁建记录详情
     * @params: id 主键ID
     * @params: userId 登录人ID
     * @Date:2019-06-03
     * @Return: MigrationModel 迁建记录信息
     * @Modified By:
     */
    @Override
    public MigrationModel getMigrationById(Integer id,Integer userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("userId", userId);
        map.put("isReadNo", ConstDefine.ISREAD_NO);
        map.put("isRead", ConstDefine.ISREAD);
        map.put("state", ConstDefine.STATE_ABLE);
        MigrationModel migrationModel = migrationMapper.getMigrationById(map);

        if (migrationModel != null) {
            /**查询迁建设备*/
            Map<String, Object> mapM = new HashMap<>();
            mapM.put("migrationId", migrationModel.getId());
            mapM.put("state", ConstDefine.STATE_ABLE);
            List<MigrationCameraModel> migrationCameraModels = migrationCameraMapper.getMigrationCameraByMigrationId(mapM);
            if (migrationCameraModels.size() > 0) {
                for (int i = 0; i < migrationCameraModels.size(); i++) {
                    if(migrationCameraModels.get(i).getGroupId() != null && migrationCameraModels.get(i).getGroupId() != 0) {
                        String groupName = cameraGroupService.getCameraGroupNameById(migrationCameraModels.get(i).getGroupId());
                        migrationCameraModels.get(i).setGroupName(groupName);
                    }
                }
                migrationModel.setMigrationCameraModels(migrationCameraModels);
            }

            /**如果迁建记录为未读消息，修改状态为已读*/
            if(migrationModel.getIsRead()==ConstDefine.ISREAD_NO){
                Map<String, Object> mapI = new HashMap<>();
                mapI.put("migrationId", id);
                mapI.put("userId", userId);
                migrationInformMapper.deleteMigrationInform(mapI);
            }
        }
        return migrationModel;
    }

    /**
     * @Author:cat
     * @Description 新增迁建记录信息
     * @params: organAddVO 迁建记录信息
     * @Date:2019-06-01
     * @Return: Integer 主键
     * @Modified By:
     */
    @Override
    @Transactional
    public Integer addMigration(MigrationAddVO migrationAddVO) {
        Integer iRet = migrationMapper.addMigration(migrationAddVO);
        int id = 0;
        if (iRet > 0) {
            /**新增迁建设备信息*/
            if (!StringUtils.isBlank(migrationAddVO.getCameraIds())) {
                String cameraIds[] = migrationAddVO.getCameraIds().split(",");

                for (int i = 0; i < cameraIds.length; i++) {
                    MigrationCameraAddVO migrationCameraAddVO = new MigrationCameraAddVO();
                    migrationCameraAddVO.setMigrationId(migrationAddVO.getId());
                    migrationCameraAddVO.setCameraId(Integer.parseInt(cameraIds[i]));
                    migrationCameraMapper.addMigrationCamera(migrationCameraAddVO);
                }
                id = migrationAddVO.getId();
            }else {
                throw new ServiceException(StatusDefineMessage.GetMessage(StatusDefine.CAMERA_NULL));
            }
        }
        return id;
    }

    /**
     * @Author:cat
     * @Description 删除迁建记录信息
     * @params: map 查询条件Map
     * @Date:2019-06-03
     * @Return: Integer 影响行数
     * @Modified By:
     */
    @Override
    public Integer deleteMigration(Map<String, Object> map) {
        /**判断是否为待审批的申请*/
        Map<String, Object> mapM = new HashMap<>();
        mapM.put("id", map.get("id"));
        mapM.put("userId", map.get("editUser"));
        mapM.put("isReadNo", ConstDefine.ISREAD_NO);
        mapM.put("isRead", ConstDefine.ISREAD);
        mapM.put("state", ConstDefine.STATE_ABLE);
        MigrationModel migrationModel = migrationMapper.getMigrationById(mapM);
        if(migrationModel.getStatus()!=ConstDefine.APPROVE_STATUS_WAIT) {
            throw new ServiceException(StatusDefineMessage.GetMessage(StatusDefine.APPLICATION_DELETE));
        }

        return migrationMapper.deleteMigration(map);
    }

    /**
     * @Author:cat
     * @Description 查询迁建记录未读消息总数
     * @params: map 查询条件
     * @Date:2019-06-03
     * @Return: Long 数据总条数
     * @Modified By:
     */
    @Override
    public Long getMigrationIsNotRead(Map<String, Object> map) {

        return migrationInformMapper.getMigrationInformByUserId(map);
    }

}
